package edu.gatech.cs2340.mule.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeListener;

import edu.gatech.cs2340.mule.presenter.MuleGamePresenter;

/**
 * This class is the panel where the user will sell their resources back to the
 * store. They can select which resource they would like to sell, the amount
 * they are willing to sell, and then commit the transaction.
 * 
 * @author Zach Elliott
 */
public class SellPanel extends JPanel {
	private static final int HGAP = 50;
	private static final int VGAP = 50;
	
	private JButton sellButton;
	private JButton cancelButton;
	private JComboBox resourceSelectionComboBox;
	private JSpinner resourceAmountSpinner;
	private JLabel priceLabel;
	
	/**
	 * Initializes the panel.
	 */
	public SellPanel() {
		GridLayout layout = new GridLayout(3, 2);
		layout.setHgap(HGAP);
		layout.setVgap(VGAP);
		
		setLayout(layout);
		setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		
		resourceSelectionComboBox = new JComboBox();
		resourceSelectionComboBox.setModel(new DefaultComboBoxModel(new String[]
				{"Food", "Energy", "Smithore"}));
		resourceSelectionComboBox.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		resourceSelectionComboBox.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		add(resourceSelectionComboBox);
		
		resourceAmountSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 8, 1));
		resourceAmountSpinner.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		resourceAmountSpinner.setBackground(MuleGamePresenter.BACKGROUND_COLOR);		
		//Recolor each component of the spinner
		for(Component c : resourceAmountSpinner.getComponents()) {
			c.setBackground(MuleGamePresenter.FOREGROUND_COLOR);
			c.setForeground(MuleGamePresenter.BACKGROUND_COLOR);
		}
		add(resourceAmountSpinner);
		
		sellButton = new JButton();
		sellButton.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		sellButton.setBorder(new LineBorder(MuleGamePresenter.BACKGROUND_COLOR, 2));
		sellButton.setIcon(new ImageIcon("Images\\icon_sell.png"));
		add(sellButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setBackground(MuleGamePresenter.FOREGROUND_COLOR);
		cancelButton.setForeground(MuleGamePresenter.BACKGROUND_COLOR);
		add(cancelButton);
		
		priceLabel = new JLabel("<html><font size=+2>Sell Value: 0");
		priceLabel.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(priceLabel);
	}
	
	/**
	 * 
	 * @param a the action listener to be added to the sell button
	 */
	public void addSellListener(ActionListener a) {
		sellButton.addActionListener(a);
	}
	
	/**
	 * 
	 * @param a the action listener to be added to the cancel button
	 */
	public void addCancelListener(ActionListener a) {
		cancelButton.addActionListener(a);
	}
	
	/**
	 * Updates the price label so the user can see how much the current
	 * transaction will cost.
	 * @param price the price of the current transaction
	 */
	public void updateSellPanelPriceLabel(int price) {
		priceLabel.setText("<html><font size=+2>Sell Value: " + price);
	}
	
	/**
	 * 
	 * @param c the change listener to be added to the spinner
	 */
	public void addSpinnerListener(ChangeListener c) {
		resourceAmountSpinner.addChangeListener(c);
	}
	
	/**
	 * 
	 * @param i the item listener to be added to the combo box
	 */
	public void addComboListener(ItemListener i) {
		resourceSelectionComboBox.addItemListener(i);
	}
	
	/**
	 * Update the maximum value allowed for the spinner
	 * @param amount the spinner's new maximum value
	 */
	public void updateSellPanelSpinnerMax(int amount) {
		resourceAmountSpinner.setModel(new SpinnerNumberModel(0,0,amount,1));
	}
	
	/**
	 * 
	 * @return the current resource type selected by the user with
	 * the combo box
	 */
	public String getSelectedResource() {
		return (String) resourceSelectionComboBox.getSelectedItem();
	}
	
	/**
	 * 
	 * @return the current amount of resources to be sold
	 */
	public int getSelectedAmount() {
		return (int) resourceAmountSpinner.getValue();
	}
}
